package cn.itcast.quartz;

import cn.itcast.domain.system.User;
import cn.itcast.service.system.UserService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SendBirthdayJob {

    @Autowired
    private AmqpTemplate template;
    @Autowired
    private UserService userService;

    public void send(){
        //1.查询数据库,获得今天生日的员工列表(应包含每位员工的姓名,手机号,邮箱)
        //2.遍历列表获得每一位员工对象
        //3.使用消息阵列发送短信和邮箱

        //查询当天生日的用户列表
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        String today = sdf.format(new Date());
        List<User> userList = userService.findByBirthday(today);
        Map<String,String> smsMap = new HashMap();
        Map<String, String> emailMap = new HashMap();
        //判断是否为空
        if (!userList.isEmpty()){
            for (User user : userList) {
                String phone = user.getTelephone();
                String email = user.getEmail();

                //定时信息发送
                smsMap.put("phone",phone);
                //转换并发送消息
                template.convertAndSend("sms.birthday",smsMap);

                //定时邮件发送
                emailMap.put("to",email);
                emailMap.put("title","亲爱的员工,生日快乐!");
                emailMap.put("content","你好，这是一封测试邮件");
                //转换并发送消息
                template.convertAndSend("mail.birthday",emailMap);
            }
        }

    }
}
